require "Dialogs"

Arena.Resize(480, 130)
timer = 0
tableaux = 0

state = 1
maxjump = 52
jump = 0
newpX = Player.absx
newpY = Player.absy

SetPPCollision(true)
Black1 = CreateSprite("Bullets/Bones/Body","Top")
Black1.color = {0, 0, 0}
Black1.Scale(1/5*200, Arena.height + 2)
Black1.MoveTo(665, 160)
Black2 = CreateSprite("Bullets/Bones/Body","Top")
Black2.color = {0, 0, 0}
Black2.Scale(1/5*200, Arena.height + 2)
Black2.MoveTo(-25, 160)

Bones = {}

function RecolorSoul()
	if state ~= 1 then
		Player.sprite.color = {1.0,0.0,0.0}
		Player.sprite.rotation = 0
	elseif state == 1 then
		Player.sprite.color = {0,60/255,1}
		Player.sprite.rotation = 0
	end
end

RecolorSoul()

function Update()
	DialogUpdate()
	if not timerEnd or Bone then
		timer = timer + 1
		if timer == 40 then
			aux = 169
			for i=1,25 do
				if i >= 4 and i <= 13 then
					aux = aux + 4
				elseif i > 13 and i <= 23 then
					aux = aux - 4
				end
				Body = CreateProjectileAbs("Bullets/Bones/Body", 568 + 12*i, aux)
				if i >= 7 and i <= 13 then
					Body.sprite.yscale = 112 - 4*i
					Body.MoveToAbs(Body.absx, aux + 12 - 2*i)
					Body.SetVar("legs", CreateProjectileAbs("Bullets/Bones/Head", Body.absx, aux + 12 - 2*i - Body.sprite.yscale/2 - 2.5))
				elseif i > 13 and i <= 20 then
					Body.sprite.yscale = 8 + 4*i
					Body.MoveToAbs(Body.absx, aux - 40 + 2*i)
					Body.SetVar("legs", CreateProjectileAbs("Bullets/Bones/Head", Body.absx, aux - 40 + 2*i - Body.sprite.yscale/2 - 2.5))
				else
					Body.sprite.yscale = 88
					Body.SetVar("legs", CreateProjectileAbs("Bullets/Bones/Head", Body.absx, aux - Body.sprite.yscale/2 - 2.5))
				end
				Body.GetVar("legs").sprite.rotation = 180
				if (i >= 1 and i <= 5) or (i >= 21 and i <= 25) then
					Body.SetVar("head", CreateProjectileAbs("Bullets/Bones/Head", Body.absx, aux + Body.sprite.yscale/2 + 2.5))
				end
				Body.SetVar("vel", -8)
				table.insert(Bones, Body)
				if i >= 6 and i <= 13 then
					Body = CreateProjectileAbs("Bullets/Bones/Body", 568 + 12*i, 83 + 2*i)
					Body.sprite.yscale = -24 + 4*i
					Body.SetVar("head", CreateProjectileAbs("Bullets/Bones/Head", Body.absx, Body.absy + Body.sprite.yscale/2 + 2.5))
					Body.SetVar("vel", -8)
					table.insert(Bones, Body)
				elseif i > 13 and i <= 20 then
					Body = CreateProjectileAbs("Bullets/Bones/Body", 568 + 12*i, 135 - 2*i)
					Body.sprite.yscale = 80 - 4*i
					Body.SetVar("head", CreateProjectileAbs("Bullets/Bones/Head", Body.absx, Body.absy + Body.sprite.yscale/2 + 2.5))
					Body.SetVar("vel", -8)
					table.insert(Bones, Body)
				end
			end
		elseif timer == 160 then
			aux = 169
			for i=1,25 do
				if i >= 4 and i <= 13 then
					aux = aux + 4
				elseif i > 13 and i <= 23 then
					aux = aux - 4
				end
				Body = CreateProjectileAbs("Bullets/Bones/Body", 72 - 12*i, aux)
				if i >= 7 and i <= 13 then
					Body.sprite.yscale = 112 - 4*i
					Body.MoveToAbs(Body.absx, aux + 12 - 2*i)
					Body.SetVar("legs", CreateProjectileAbs("Bullets/Bones/Head", Body.absx, aux + 12 - 2*i - Body.sprite.yscale/2 - 2.5))
				elseif i > 13 and i <= 20 then
					Body.sprite.yscale = 8 + 4*i
					Body.MoveToAbs(Body.absx, aux - 40 + 2*i)
					Body.SetVar("legs", CreateProjectileAbs("Bullets/Bones/Head", Body.absx, aux - 40 + 2*i - Body.sprite.yscale/2 - 2.5))
				else
					Body.sprite.yscale = 88
					Body.SetVar("legs", CreateProjectileAbs("Bullets/Bones/Head", Body.absx, aux - Body.sprite.yscale/2 - 2.5))
				end
				Body.GetVar("legs").sprite.rotation = 180
				if (i >= 1 and i <= 5) or (i >= 21 and i <= 25) then
					Body.SetVar("head", CreateProjectileAbs("Bullets/Bones/Head", Body.absx, aux + Body.sprite.yscale/2 + 2.5))
				end
				Body.SetVar("vel", 8)
				table.insert(Bones, Body)
				if i >= 6 and i <= 13 then
					Body = CreateProjectileAbs("Bullets/Bones/Body", 72 - 12*i, 83 + 2*i)
					Body.sprite.yscale = -24 + 4*i
					Body.SetVar("head", CreateProjectileAbs("Bullets/Bones/Head", Body.absx, Body.absy + Body.sprite.yscale/2 + 2.5))
					Body.SetVar("vel", 8)
					table.insert(Bones, Body)
				elseif i > 13 and i <= 20 then
					Body = CreateProjectileAbs("Bullets/Bones/Body", 72 - 12*i, 135 - 2*i)
					Body.sprite.yscale = 80 - 4*i
					Body.SetVar("head", CreateProjectileAbs("Bullets/Bones/Head", Body.absx, Body.absy + Body.sprite.yscale/2 + 2.5))
					Body.SetVar("vel", 8)
					table.insert(Bones, Body)
				end
			end
		elseif timer == 280 then
			aux = 169
			for i=1,25 do
				if i >= 4 and i <= 13 then
					aux = aux + 4
				elseif i > 13 and i <= 23 then
					aux = aux - 4
				end
				Body = CreateProjectileAbs("Bullets/Bones/Body", 568 + 12*i, aux)
				if i >= 7 and i <= 13 then
					Body.sprite.yscale = 112 - 4*i
					Body.MoveToAbs(Body.absx, aux + 12 - 2*i)
					Body.SetVar("legs", CreateProjectileAbs("Bullets/Bones/Head", Body.absx, aux + 12 - 2*i - Body.sprite.yscale/2 - 2.5))
				elseif i > 13 and i <= 20 then
					Body.sprite.yscale = 8 + 4*i
					Body.MoveToAbs(Body.absx, aux - 40 + 2*i)
					Body.SetVar("legs", CreateProjectileAbs("Bullets/Bones/Head", Body.absx, aux - 40 + 2*i - Body.sprite.yscale/2 - 2.5))
				else
					Body.sprite.yscale = 88
					Body.SetVar("legs", CreateProjectileAbs("Bullets/Bones/Head", Body.absx, aux - Body.sprite.yscale/2 - 2.5))
				end
				Body.GetVar("legs").sprite.rotation = 180
				if (i >= 1 and i <= 5) or (i >= 21 and i <= 25) then
					Body.SetVar("head", CreateProjectileAbs("Bullets/Bones/Head", Body.absx, aux + Body.sprite.yscale/2 + 2.5))
				end
				Body.SetVar("vel", -8)
				table.insert(Bones, Body)
				if i >= 6 and i <= 13 then
					Body = CreateProjectileAbs("Bullets/Bones/Body", 568 + 12*i, 83 + 2*i)
					Body.sprite.yscale = -24 + 4*i
					Body.SetVar("head", CreateProjectileAbs("Bullets/Bones/Head", Body.absx, Body.absy + Body.sprite.yscale/2 + 2.5))
					Body.SetVar("vel", -8)
					table.insert(Bones, Body)
				elseif i > 13 and i <= 20 then
					Body = CreateProjectileAbs("Bullets/Bones/Body", 568 + 12*i, 135 - 2*i)
					Body.sprite.yscale = 80 - 4*i
					Body.SetVar("head", CreateProjectileAbs("Bullets/Bones/Head", Body.absx, Body.absy + Body.sprite.yscale/2 + 2.5))
					Body.SetVar("vel", -8)
					table.insert(Bones, Body)
				end
			end
			aux = 169
			for i=1,25 do
				if i >= 4 and i <= 13 then
					aux = aux + 4
				elseif i > 13 and i <= 23 then
					aux = aux - 4
				end
				Body = CreateProjectileAbs("Bullets/Bones/Body", 72 - 12*i, aux)
				if i >= 7 and i <= 13 then
					Body.sprite.yscale = 112 - 4*i
					Body.MoveToAbs(Body.absx, aux + 12 - 2*i)
					Body.SetVar("legs", CreateProjectileAbs("Bullets/Bones/Head", Body.absx, aux + 12 - 2*i - Body.sprite.yscale/2 - 2.5))
				elseif i > 13 and i <= 20 then
					Body.sprite.yscale = 8 + 4*i
					Body.MoveToAbs(Body.absx, aux - 40 + 2*i)
					Body.SetVar("legs", CreateProjectileAbs("Bullets/Bones/Head", Body.absx, aux - 40 + 2*i - Body.sprite.yscale/2 - 2.5))
				else
					Body.sprite.yscale = 88
					Body.SetVar("legs", CreateProjectileAbs("Bullets/Bones/Head", Body.absx, aux - Body.sprite.yscale/2 - 2.5))
				end
				Body.GetVar("legs").sprite.rotation = 180
				if (i >= 1 and i <= 5) or (i >= 21 and i <= 25) then
					Body.SetVar("head", CreateProjectileAbs("Bullets/Bones/Head", Body.absx, aux + Body.sprite.yscale/2 + 2.5))
				end
				Body.SetVar("vel", 8)
				table.insert(Bones, Body)
				if i >= 6 and i <= 13 then
					Body = CreateProjectileAbs("Bullets/Bones/Body", 72 - 12*i, 83 + 2*i)
					Body.sprite.yscale = -24 + 4*i
					Body.SetVar("head", CreateProjectileAbs("Bullets/Bones/Head", Body.absx, Body.absy + Body.sprite.yscale/2 + 2.5))
					Body.SetVar("vel", 8)
					table.insert(Bones, Body)
				elseif i > 13 and i <= 20 then
					Body = CreateProjectileAbs("Bullets/Bones/Body", 72 - 12*i, 135 - 2*i)
					Body.sprite.yscale = 80 - 4*i
					Body.SetVar("head", CreateProjectileAbs("Bullets/Bones/Head", Body.absx, Body.absy + Body.sprite.yscale/2 + 2.5))
					Body.SetVar("vel", 8)
					table.insert(Bones, Body)
				end
			end
		elseif timer == 400 then
			Bone = CreateProjectileAbs("Bullets/Bones/Body", 320, 225)
			Bone.sprite.color = {1, 150/255, 0}
			Bone.sprite.yscale = 32
			Bone.sprite.rotation = 90
			Bone.SetVar("Y", -4)
			Bone.SetVar("tipo", "Orange")
			Bone.SetVar("head", CreateProjectileAbs("Bullets/Bones/Head", 301.5, 225))
			Bone.GetVar("head").sprite.rotation = 90
			Bone.GetVar("head").sprite.color = {1, 150/255, 0}
			Bone.GetVar("head").SetVar("tipo", "Orange")
			Bone.SetVar("legs", CreateProjectileAbs("Bullets/Bones/Head", 338.5, 225))
			Bone.GetVar("legs").sprite.rotation = -90
			Bone.GetVar("legs").sprite.color = {1, 150/255, 0}
			Bone.GetVar("legs").SetVar("tipo", "Orange")
			aux = 169
			for i=1,25 do
				if i >= 4 and i <= 13 then
					aux = aux + 4
				elseif i > 13 and i <= 23 then
					aux = aux - 4
				end
				Body = CreateProjectileAbs("Bullets/Bones/Body", 568 + 12*i, aux)
				if i >= 7 and i <= 13 then
					Body.sprite.yscale = 112 - 4*i
					Body.MoveToAbs(Body.absx, aux + 12 - 2*i)
					Body.SetVar("legs", CreateProjectileAbs("Bullets/Bones/Head", Body.absx, aux + 12 - 2*i - Body.sprite.yscale/2 - 2.5))
				elseif i > 13 and i <= 20 then
					Body.sprite.yscale = 8 + 4*i
					Body.MoveToAbs(Body.absx, aux - 40 + 2*i)
					Body.SetVar("legs", CreateProjectileAbs("Bullets/Bones/Head", Body.absx, aux - 40 + 2*i - Body.sprite.yscale/2 - 2.5))
				else
					Body.sprite.yscale = 88
					Body.SetVar("legs", CreateProjectileAbs("Bullets/Bones/Head", Body.absx, aux - Body.sprite.yscale/2 - 2.5))
				end
				Body.GetVar("legs").sprite.rotation = 180
				if (i >= 1 and i <= 5) or (i >= 21 and i <= 25) then
					Body.SetVar("head", CreateProjectileAbs("Bullets/Bones/Head", Body.absx, aux + Body.sprite.yscale/2 + 2.5))
				end
				Body.SetVar("vel", -8)
				table.insert(Bones, Body)
				if i >= 6 and i <= 13 then
					Body = CreateProjectileAbs("Bullets/Bones/Body", 568 + 12*i, 83 + 2*i)
					Body.sprite.yscale = -24 + 4*i
					Body.SetVar("head", CreateProjectileAbs("Bullets/Bones/Head", Body.absx, Body.absy + Body.sprite.yscale/2 + 2.5))
					Body.SetVar("vel", -8)
					table.insert(Bones, Body)
				elseif i > 13 and i <= 20 then
					Body = CreateProjectileAbs("Bullets/Bones/Body", 568 + 12*i, 135 - 2*i)
					Body.sprite.yscale = 80 - 4*i
					Body.SetVar("head", CreateProjectileAbs("Bullets/Bones/Head", Body.absx, Body.absy + Body.sprite.yscale/2 + 2.5))
					Body.SetVar("vel", -8)
					table.insert(Bones, Body)
				end
			end
			aux = 169
			for i=1,25 do
				if i >= 4 and i <= 13 then
					aux = aux + 4
				elseif i > 13 and i <= 23 then
					aux = aux - 4
				end
				Body = CreateProjectileAbs("Bullets/Bones/Body", 72 - 12*i, aux)
				if i >= 7 and i <= 13 then
					Body.sprite.yscale = 112 - 4*i
					Body.MoveToAbs(Body.absx, aux + 12 - 2*i)
					Body.SetVar("legs", CreateProjectileAbs("Bullets/Bones/Head", Body.absx, aux + 12 - 2*i - Body.sprite.yscale/2 - 2.5))
				elseif i > 13 and i <= 20 then
					Body.sprite.yscale = 8 + 4*i
					Body.MoveToAbs(Body.absx, aux - 40 + 2*i)
					Body.SetVar("legs", CreateProjectileAbs("Bullets/Bones/Head", Body.absx, aux - 40 + 2*i - Body.sprite.yscale/2 - 2.5))
				else
					Body.sprite.yscale = 88
					Body.SetVar("legs", CreateProjectileAbs("Bullets/Bones/Head", Body.absx, aux - Body.sprite.yscale/2 - 2.5))
				end
				Body.GetVar("legs").sprite.rotation = 180
				if (i >= 1 and i <= 5) or (i >= 21 and i <= 25) then
					Body.SetVar("head", CreateProjectileAbs("Bullets/Bones/Head", Body.absx, aux + Body.sprite.yscale/2 + 2.5))
				end
				Body.SetVar("vel", 8)
				table.insert(Bones, Body)
				if i >= 6 and i <= 13 then
					Body = CreateProjectileAbs("Bullets/Bones/Body", 72 - 12*i, 83 + 2*i)
					Body.sprite.yscale = -24 + 4*i
					Body.SetVar("head", CreateProjectileAbs("Bullets/Bones/Head", Body.absx, Body.absy + Body.sprite.yscale/2 + 2.5))
					Body.SetVar("vel", 8)
					table.insert(Bones, Body)
				elseif i > 13 and i <= 20 then
					Body = CreateProjectileAbs("Bullets/Bones/Body", 72 - 12*i, 135 - 2*i)
					Body.sprite.yscale = 80 - 4*i
					Body.SetVar("head", CreateProjectileAbs("Bullets/Bones/Head", Body.absx, Body.absy + Body.sprite.yscale/2 + 2.5))
					Body.SetVar("vel", 8)
					table.insert(Bones, Body)
				end
			end
		elseif timer > 400 and timer < 520 then
			if Bone then
				Bone.Move(0, Bone.GetVar("Y"))
				Bone.GetVar("head").Move(0, Bone.GetVar("Y"))
				Bone.GetVar("legs").Move(0, Bone.GetVar("Y"))
				Bone.SetVar("Y", Bone.GetVar("Y") - 0.4)
				if Bone.absy <= 101.5 then
					if not Bone.GetVar("Rebotado") then
						Bone.SetVar("Y", 6)
						Bone.SetVar("Rebotado", 1)
						Bone.MoveToAbs(320, 102)
						Bone.GetVar("head").MoveToAbs(301.5, 102)
						Bone.GetVar("legs").MoveToAbs(338.5, 102)
					elseif Bone.GetVar("Rebotado") == 1 then
						Bone.SetVar("Y", 5)
						Bone.SetVar("Rebotado", 2)
						Bone.MoveToAbs(320, 102)
						Bone.GetVar("head").MoveToAbs(301.5, 102)
						Bone.GetVar("legs").MoveToAbs(338.5, 102)
					elseif Bone.GetVar("Rebotado") == 2 then
						Bone.Remove()
						Bone.GetVar("head").Remove()
						Bone.GetVar("legs").Remove()
						Bone = nil
					end
				end
			end
		elseif timer == 520 then
			Black1.Remove()
			Black2.Remove()
			EndWave()
		end
	else
		if timerEnd < 180 then
			timerEnd = timerEnd + 1
			if timerEnd == 10 then
				Encounter["GameMusic"] = "Fade"
			end
		else
			Black1.Remove()
			Black2.Remove()
			EndWave()
		end
	end
	for i=1,#Bones do
		local bullet = Bones[i - tableaux]
		bullet.Move(bullet.GetVar("vel"), 0)
		if bullet.GetVar("head") then
			bullet.GetVar("head").Move(bullet.GetVar("vel"), 0)
		end
		if bullet.GetVar("legs") then
			bullet.GetVar("legs").Move(bullet.GetVar("vel"), 0)
		end
		if (bullet.absx < 320 - Arena.width/2 and bullet.GetVar("vel") < 0) or (bullet.absx > 320 + Arena.width/2 and bullet.GetVar("vel") > 0) then
			bullet.Remove()
			if bullet.GetVar("head") then
				bullet.GetVar("head").Remove()
			end
			if bullet.GetVar("legs") then
				bullet.GetVar("legs").Remove()
			end
			table.remove(Bones, i - tableaux)
			tableaux = tableaux + 1
		end
	end
	tableaux = 0
	if state == 1 then -- Down
		if Input.Right > 0 then
			newpX = newpX + 3
		end
		if Input.Left > 0 then
			newpX = newpX - 3
		end
		if newpY < 105 then
			if Input.Up > 0 then
				jump = maxjump
			else
				newpY = 103
				jump = 0
			end
		elseif jump < 3 and jump > -3 then
			jump = jump - 1
		elseif jump < -3 and jump > -10 then
			jump = -10
		elseif jump < -3 then
			jump = jump - 3
		else
			jump = (jump/1.001) - 2
		end
		if Input.Up < 0 and jump > 6 then
			jump = 4
		end
		newpY = math.max(newpY + (jump/10), 103)
		if newpX < 328 - Arena.width/2 then
			newpX = 328 - Arena.width/2
		elseif newpX > 312 + Arena.width/2 then
			newpX = 312 + Arena.width/2
		end
		if shake then
			Player.MoveToAbs(newpX + shake[1], newpY + shake[2], true)
		else
			Player.MoveToAbs(newpX, newpY, true)
		end
    end
end

function OnHit(bullet)
	if not bullet.GetVar("No Damage") and not Player.isHurting then
		if bullet.GetVar("tipo") then
			if bullet.GetVar("tipo") == "Blue" and (Input.Left > 0 or Input.Right > 0 or jump ~= 0) then
				if Player.hp == 1 then
					Player.Hurt(0, 2)
					Encounter["GameOver"] = true
					timerEnd = 0
				end
				if Encounter["HPEnvenenadosPorKR"] == Player.hp - 1 and Player.hp > 1 then
					Player.hp = Player.hp - 1
					Player.Hurt(0, 2/60)
				elseif Player.hp > 1 then
					Player.Hurt(0, 2/60)
					if Encounter["Glow"].alpha >= 0.9 then
						Encounter.Call("AddKR", 2)
					elseif Encounter["Glow"].alpha >= 0.6 then
						Encounter.Call("AddKR", 1.5)
					elseif Encounter["Glow"].alpha >= 0.3 then
						Encounter.Call("AddKR", 1)
					end
				end
				Encounter["NOHIT"] = nil
			elseif bullet.GetVar("tipo") == "Orange" and (Input.Left <= 0 and Input.Right <= 0 and jump == 0) then
				if Player.hp == 1 then
					Player.Hurt(0, 2)
					Encounter["GameOver"] = true
					timerEnd = 0
				end
				if Encounter["HPEnvenenadosPorKR"] == Player.hp - 1 and Player.hp > 1 then
					Player.hp = Player.hp - 1
					Player.Hurt(0, 2/60)
				elseif Player.hp > 1 then
					Player.Hurt(0, 2/60)
					if Encounter["Glow"].alpha >= 0.9 then
						Encounter.Call("AddKR", 2)
					elseif Encounter["Glow"].alpha >= 0.6 then
						Encounter.Call("AddKR", 1.5)
					elseif Encounter["Glow"].alpha >= 0.3 then
						Encounter.Call("AddKR", 1)
					end
				end
				Encounter["NOHIT"] = nil
			end
		else
			if Player.hp == 1 then
				Player.Hurt(0, 2)
				Encounter["GameOver"] = true
				timerEnd = 0
			end
			if Encounter["HPEnvenenadosPorKR"] == Player.hp - 1 and Player.hp > 1 then
				Player.hp = Player.hp - 1
				Player.Hurt(0, 2/60)
			elseif Player.hp > 1 then
				Player.Hurt(0, 2/60)
				if Encounter["Glow"].alpha >= 0.9 then
					Encounter.Call("AddKR", 2)
				elseif Encounter["Glow"].alpha >= 0.6 then
					Encounter.Call("AddKR", 1.5)
				elseif Encounter["Glow"].alpha >= 0.3 then
					Encounter.Call("AddKR", 1)
				end
			end
			Encounter["NOHIT"] = nil
		end
	end
end